/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef __MAINDIALOG_H__
#define __MAINDIALOG_H__

#include "mist.h"
#include "ProcessThread.h"
#include "QIplImage.h"
#include "MouseControl.h"
#include "SettingDlg.h"
#include "Configuration.h"
#include "DisplayControler.h"

class QEvent;

/**
 * The main dialog of CoCon, where the user read comics.
 * 
 * @author reach (5/26/2010)
 */
class MainDialog : public QDialog
{
    Q_OBJECT

    //-- macro for installing mouse controler
    CO_MOUSECONTROL
public slots:
    void mouseCtlEvent(GestureEvent* event);

    //-- constructor and destructor
public:
    MainDialog(QWidget *parent = 0);
    ~MainDialog();

    //-- public slots
public slots:
    void setGrayscale();
    bool openComic(const QString& filename);
    void prevComic();
    void nextComic();
    void requestPrevPage();
    void requestNextPage();
    void requestPage(int iPage, int iDivPage = 0, bool isPrev = false);

    //-- private slots
private slots:
    bool event(QEvent* event); 
    void paintEvent(QPaintEvent* e);
    void keyPressEvent(QKeyEvent* event);
    void updatePage(DivPage* page);
    bool eventFilter(QObject* obj, QEvent* evt);
    void shutdown();
    void wakeup();
    void OnScale();
    void OnThresh();
    void OnContour();
    void OnLine();
    void OnScroll();

    //-- private memthods
private:
    void setOperations();
    void initUI();
    void connectSignals();
    void refreshScreen(ScreenProxy::Waveform w = ScreenProxy::INVALID);
    void updateWaveform();
    void fileBrowse();
    void gotoPage();
    void setting();
    void changeToOrigin(const QPoint& pos = QPoint(0, 0));
    void runOperation(int type, QKeyEvent* keyEvent = NULL, GestureEvent* mouseEvent = NULL);

    //-- private members
private:
    // profile manager
    CoconProfile* m_profile;

    // page manage
    QDir m_dir;
    QStringList m_sFileNames;
    ProcessThread* m_thread;
    QVector<QRgb> m_colorTable;

    // control arguments
    bool m_isPrev;
    int m_startScreenDegree;

    // setting dialog
    SettingDlg* m_settingDlg;

    // UI
    QHBoxLayout* m_hlayout;
    QPushButton* m_bScale;
    QPushButton* m_bThresh;
    QPushButton* m_bContour;
    QPushButton* m_bLine;
    QPushButton* m_bScroll;

    // display controler
    DisplayControler* m_displayControler;

    // flush argument
    ScreenProxy::Waveform m_waveForm;
    int m_flushCount;
    QTimer m_timerUpdate;
};

#endif // __MAINDIALOG_H__
